package com.itheima.web.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.cargo.Export;
import com.itheima.domain.cargo.Packing;
import com.itheima.domain.cargo.Shipping;
import com.itheima.domain.cargo.ShippingExample;
import com.itheima.service.cargo.ExportService;
import com.itheima.service.cargo.PackingService;
import com.itheima.service.cargo.ShippingService;
import com.itheima.web.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/cargo/shipping")
public class ShippingController extends BaseController {

    @Reference
    private PackingService packingService;  //装箱单

    @Reference
    private ShippingService shippingService; //委托单

    @Reference
    private ExportService exportService; //报运单




    @RequestMapping(value = "/toShipping", name = "跳转委托书新增页面")
    public String toShipping(String id) {

        request.setAttribute("id", id);
        Packing packing = packingService.findById(id);
        String exportIds = packing.getExportIds();

        Shipping shipping = new Shipping();
        String[] split = exportIds.split(",");
        //有问题,涉及多个报运单的起始港和目的港显示问题,最后只显示一个起始港和目的港
        for (String exportId : split) {
            Export export = exportService.findById(exportId);
            if (export!=null) {
                shipping.setCredit(export.getLcno());
                shipping.setMarks(export.getMarks());
                shipping.setShipmentPort(export.getShipmentPort());
                shipping.setDestinationPort(export.getDestinationPort());
            }
        }

        request.setAttribute("shipping", shipping);
        return "/cargo/shipping/shipping-add";


    }


    //添加 或 修改
    @RequestMapping("/edit")
    public String edit(Shipping shipping){
        System.out.println(shipping);
        shipping.setState(0);
        shippingService.save(shipping);

//        if (shipping.getShippingListId()) {
//            //2. 设置企业信息
//            shipping.setCreateBy(getUser().getUserName());
//            shipping.setCreateBydate(getUser().getCreateTime());
//            shipping.setCreateBydept(getUser().getCreateDept());
//            //3.制单时间
//            shippingService.save(shipping);
//        } else {
//            shippingService.update(shipping);
//        }
//        shipping.setShippingListId(UUID.randomUUID().toString());
        return "redirect:/cargo/shipping/list.do";
    }
    //分页显示
    @RequestMapping("/list")
    public String list(
            @RequestParam(defaultValue = "1", name = "page") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize){
        ShippingExample shippingExample = new ShippingExample();
        PageInfo info = shippingService.findByPage(pageNum, pageSize,shippingExample);
        request.setAttribute("page", info);
        return "/cargo/shipping/shipping-list";
    }


    @RequestMapping(value = "/delete", name = "委托单删除")
    public String delete(String id) {
        //调用service删除
        Shipping shipping = shippingService.findById(id);

        if (shipping!=null) {
            if (shipping.getState() == 0) {
                shippingService.delete(id);
            }
        }

        //重定向到list方法
        return "redirect:/cargo/shipping/list.do";
    }
}
